package org.csu.petserver.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.petserver.domain.Category;
import org.csu.petserver.domain.Product;
import org.csu.petserver.persistence.CategoryMapper;
import org.csu.petserver.persistence.ProductMapper;
import org.csu.petserver.service.CategoryInfo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("CategoryInfo")
@MapperScan("org.csu.petserver.persistence")
public class CategoryInfoImpl implements CategoryInfo {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Override
    public List<Category> getAll() {
        return categoryMapper.selectList(null);
    }

    @Override
    public List<Product> getAllProductByCategory(Category category) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category.getId());
        return productMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateCategory(Category category) {
        categoryMapper.updateById(category);
        return true;
    }

    @Override
    public List<Product> getAllProductByCategoryId(String categoryId) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",categoryId);
        return productMapper.selectList(queryWrapper);
    }
}
